package com.bnz.service.impl;

import com.bnz.entity.Users;
import com.bnz.mapper.UsersMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;

public class MyUserDetailService implements UserDetailsService {
    // 引入mapper
    @Autowired
    private UsersMapper usersMapper;

    @Override
    public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
        // 1、根据用户名来查询出用户
        Users users = new Users();
        users.setUsername(s);
        Users selectOne = usersMapper.selectOne(users);
        // 2、判断如果不存在，抛异常
        if (selectOne == null){
            throw new UsernameNotFoundException("该用户不存在");
        }
        return new User(s,users.getPassword(), AuthorityUtils.commaSeparatedStringToAuthorityList("role,userxx,select,admin,ROLE_user,ROLE_admin"));
    }
}
